package votd_notification;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import androidhive.dashboard.R;

import java.util.Calendar;


public class DailyNotification extends BroadcastReceiver {

	// Register the alarm and set it at 7am everyday (repeating mode)
	public static void registerAlarm(Context paramContext) {
		Calendar calendar = Calendar.getInstance();
//		if (calendar.get(Calendar.HOUR_OF_DAY) >= 7) {
//			calendar.add(7, 1);
//		}
		
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);

		// PendingIntent that will perform a broadcast
		PendingIntent localPendingIntent = PendingIntent
				.getBroadcast(
						paramContext,
						22341,
						new Intent(
								"votd_notification.DAILY_NOTIFICATION"),
						PendingIntent.FLAG_UPDATE_CURRENT);
		// Retrieve an AlarmManager to set a repeating daily alarm
		((AlarmManager) paramContext.getSystemService("alarm")).setRepeating(1,
				calendar.getTimeInMillis(), 24 * 60 * 60 * 1000,
				localPendingIntent);
	}

	// Remove the alarm
	public static void removeAlarm(Context paramContext) {
		// PendingIntent that will perform a broadcast
		PendingIntent localPendingIntent = PendingIntent
				.getBroadcast(
						paramContext,
						22341,
						new Intent(
								"votd_notification.DAILY_NOTIFICATION"),
						PendingIntent.FLAG_UPDATE_CURRENT);
		// Retrieve an AlarmManager to cancel the alarm
		((AlarmManager) paramContext.getSystemService("alarm"))
				.cancel(localPendingIntent);
	}

	// Show a notification in the Notification bar with the application icon,
	// the name of the application
	// Notification will launched the EnterWeightActivity once the user touches
	// it
	private void showNotification(Context paramContext) {
		NotificationManager  nm = (NotificationManager) paramContext.getSystemService(Context.NOTIFICATION_SERVICE);
	        // Get the text we passed in from ShiVa and place that text here in the message we are going to display to user
	        CharSequence message = "sText";
	        // Create a new pending intent called contentIntent
	        PendingIntent contentIntent = PendingIntent.getActivity(paramContext, 0, new Intent(), 0);
	        // Create a new notification which will include YOUR APP ICON
	        Notification notif = new Notification(R.drawable.abc_ab_bottom_solid_dark_holo, message, System.currentTimeMillis());
	        // Setup the notification window
	        CharSequence from = "YourAppNameHere";
	        notif.setLatestEventInfo(paramContext, from, message, contentIntent);
	        // This flag here will INSISTENTLY send the notification to the user until the user ACKNOWLEDGES the notification by clicking on it
	        notif.flags = Notification.FLAG_INSISTENT;
	        // This actually runs the notification command itself
	        nm.notify(1, notif);
	}

	// Override the onReceive, called when the BroadcastReceiver is receiving an
	// Intent broadcast
	@Override
	public void onReceive(Context paramContext, Intent paramIntent) {
		if (paramIntent.getAction().equals(
				"votd_notification.DAILY_NOTIFICATION"))
			showNotification(paramContext);
//		if (PreferenceManager.getDefaultSharedPreferences(paramContext)
//				.getBoolean("show_notification", false))
			registerAlarm(paramContext);

	}
}